import { axios } from "./../../utils/request"
// pages/nickname/nickname.ts
Page({
  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: "",
    navBarHeight: "",
    nickName: "",
  },
  async init() {
    let userInfo = wx.getStorageSync("userInfo")
    let { nickName } = userInfo
    this.setData({
      nickName,
      statusBarHeight: wx.getStorageSync("statusBarHeight"),
      navBarHeight: wx.getStorageSync("navBarHeight"),
    })
  },
  async changeName() {
    let nickName = this.data.nickName
    if (nickName) {
      let result = (await axios("/user/nickName", "POST", { nickName })) as any
      let userInfo = result.data[0]
      wx.setStorageSync("userInfo", userInfo)
      this.init()
      wx.showToast({
        title: "修改成功",
        icon: "success",
      })
    } else {
      wx.showToast({
        title: "请输入内容",
        icon: "none",
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.init()
  },
})
